﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using PYS.ModelViews;
using System.Diagnostics;

namespace PYS.Controllers
{
    public class ReportController : Controller
    {
     
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">The id of the course</param>
        /// <returns></returns>
        public ActionResult Report(int id)
        {
            return View(GetVoteResult(id));
        }

        public ActionResult ReportListed(int id)
        {
            return View("ReportAllVotes", GetVotes(id));
        }

        private List<VoteWorkflow> GetVoteResult(int courseID)
        {
            PYSEntities entities = new PYSEntities();
            var chTypes = (from v in entities.VoteWorkflows
                           select new { v.chName, v.chTypeID }).Distinct();


            List<VoteWorkflow> workflow = new List<VoteWorkflow>();


            foreach (var chType in chTypes)
            {
                VoteWorkflow voteResult = new VoteWorkflow();
                voteResult.chName = chType.chName;
                voteResult.chTypeID = chType.chTypeID;

                var winnerQuery = from v in entities.votes
                                  join vwf in entities.VoteWorkflows on v.chID equals vwf.chID
                                  where v.courseID == courseID && vwf.chTypeID == chType.chTypeID
                                  select new { vwf.chName, vwf.mixID, vwf.mixName, VotesCount = v.vCount } into x
                                  orderby x.VotesCount descending
                                  select x;

                foreach (var vote in winnerQuery)
                {
                    Debug.WriteLine(vote.chName + " " + vote.mixName + " " + vote.VotesCount);
                }

                var winner = winnerQuery.FirstOrDefault();

                if (winner != null)
                {
                    voteResult.mixName = winner.mixName;
                    voteResult.mixID = winner.mixID;
                    workflow.Add(voteResult);
                }
            }

            return workflow;
        }

        private List<VoteResult> GetVotes(int courseID)
        {
            PYSEntities entities = new PYSEntities();
            List<VoteResult> votes = new List<VoteResult>();

            var votesQuery = from v in entities.votes
                              join vwf in entities.VoteWorkflows on v.chID equals vwf.chID                              
                              where v.courseID == courseID
                              orderby vwf.chTypeID
                              select new {vwf.chName, vwf.mixName, v.vCount };

            foreach (var vote in votesQuery)
            {
                votes.Add(new VoteResult(vote.chName, vote.mixName, vote.vCount));
            }
            return votes;
        }
    }
}
